#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for(int i = 1; i < n; i++) {
        if(a[i] >= a[0] && b[i] >= b[0]) {
            cout << "-1\n";
            return;
        }
    }
    cout << a[0] << endl;
}

int main() {
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}